package com.ruoyi.youtian.service.impl;

import com.ruoyi.Gzmx_device.domain.Gzmx_device;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.youtian.domain.vo.FaultStatisticVo;
import com.ruoyi.youtian.mapper.FaultStatisticsMapper;
import com.ruoyi.youtian.service.FaultStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FaultStatisticsServiceImpl implements FaultStatisticsService {

    @Autowired
    private FaultStatisticsMapper faultStatisticsMapper;

    /**
     * 故障模型分析
     *
     * @param id
     * @return
     */
    @Override
    public TableDataInfo list(Integer id) {

        ArrayList<FaultStatisticVo> faultList = new ArrayList<>();

        if (id != null) {

            List<Gzmx_device> gzmx_devices = faultStatisticsMapper.selectByid(id);

            int fault = 0;
            for (Gzmx_device gzmx_device : gzmx_devices) {
                List<String> list = faultStatisticsMapper.getId(Integer.valueOf(gzmx_device.getId().toString()));
                if (list != null) {

                    FaultStatisticVo statisticVo = new FaultStatisticVo();
                    statisticVo.setTotal(gzmx_devices.size());
                    statisticVo.setBmName(gzmx_device.getBm_name());
                    statisticVo.setDevGgxh(gzmx_device.getDev_sblx_name());

                    for (String pointid : list) {
                        pointid = "异常点位id:" + pointid + "%";
                        Integer integer = faultStatisticsMapper.faultDesc(pointid);
                        if (integer > 0) {
                            fault += 1;
                        }
                    }
                    statisticVo.setQuantity(fault);
                    if (statisticVo.getQuantity() != 0 && statisticVo.getTotal() != 0) {
                        Integer total = statisticVo.getTotal();
                        Integer quantity = statisticVo.getQuantity();
                        NumberFormat numberFormat = NumberFormat.getInstance();
                        numberFormat.setMaximumFractionDigits(2);
                        String format = numberFormat.format((float) total / (float) quantity * 100);
                        statisticVo.setProportion(format + "%");
                    } else {
                        statisticVo.setProportion("0%");
                    }
                    faultList.add(statisticVo);
                }
            }
        }
        return new TableDataInfo(faultList,faultList.size());
    }

    /**
     * 饼图数据统计查询
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> PieChartByid(Integer id) {
        if (id != null) {
            List<Gzmx_device> gzmx_devices = faultStatisticsMapper.selectByid(id);
            ArrayList<FaultStatisticVo> arrayList = new ArrayList<>();
            int fault = 0;
            for (Gzmx_device Device : gzmx_devices) {
                FaultStatisticVo statisticVo = new FaultStatisticVo();
                statisticVo.setTotal(gzmx_devices.size());
                List<String> list = faultStatisticsMapper.getId(Integer.valueOf(Device.getId().toString()));

                for (String pointid : list) {
                    pointid = "异常点位id:" + pointid + "%";
                    Integer integer = faultStatisticsMapper.faultDesc(pointid);
                    if (integer > 0) {
                        fault += 1;
                    }
                }
                statisticVo.setQuantity(fault);
                arrayList.add(statisticVo);
            }
            return AjaxResult.success(arrayList);
        }
        return AjaxResult.error("参数不能为空");
    }

    /**
     * 柱状图数据统计查询
     *当前时间到未来6个月的时间的数据
     * @param id
     * @return
     */
    @Override
    public AjaxResult Histogram(Integer id) {
        //当前时间往前6个月的数据
        if (id != null) {
            List<Map<String, Object>> zonglist = new ArrayList<>();
            List<Map<String, Object>> list = faultStatisticsMapper.getHistogram(id);
            for (Map<String, Object> map : list) {
                Object redate = map.get("redate");
                if (redate != null) {
                    String string = redate.toString();
                    string = string + "月";
                    map.put("redate",string);
                }
            }
            List<Map<String, Object>> maps = faultStatisticsMapper.date(id);
            for (Map<String, Object> map : maps) {
                map.remove("redate");
                list.add(map);
            }

            return AjaxResult.success(list);
        }
        return AjaxResult.success("参数不能为空");

    }

//    /**柱状图数据统计查询
//     * 上一年现在的时间往前6个月时间的数据
//     * @param id
//     * @return
//     */
//    @Override
//    public AjaxResult ReHistogram(Integer id) {
//
//        if (id != null) {
//            //当前减去一年在往前六个月的数据
//            List<Map<String, Object>> date = faultStatisticsMapper.date(id);
//            for (Map<String, Object> map : date) {
//                Integer dayes = (Integer) map.get("dateyear");
//                if (dayes != null) {
//                    String string = dayes.toString();
//                    string = string + "月";
//                    map.put("dateyear", string);
//                }
//            }
//            return AjaxResult.success(date);
//        }
//        return AjaxResult.success("参数不能为空");
//
//    }

}
